
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include <Windows.h>

#include "GUIRTVisualisation.h"
#include "frmRenderedPicture.h"
#include "MaLoRTlib.h"

//singleton variable
GUIRTVisualisation* GUIRTVisualisation::_instance = 0;

//constructor
GUIRTVisualisation::GUIRTVisualisation()
{
	//QWidget used to display the buffer
	_frame = new frmRenderedPicture();

	//buffer storing the picture
	_buffer = 0;

	//size of the buffer
	_bufferSize = 0;

	//picture width
	_width = 0;

	//picture height
	_height = 0;
}

//destructor
GUIRTVisualisation::~GUIRTVisualisation()
{
	delete _frame;
	delete[] _buffer;
}

//get the singleton
GUIRTVisualisation& GUIRTVisualisation::getInstance()
{
	if (_instance == 0)
		_instance = new GUIRTVisualisation();

	return *_instance;
}

//delete the singleton
void GUIRTVisualisation::close()
{
	if (_instance != 0)
	{
		delete _instance;
		_instance = 0;
	}
}

//set the size of the buffer
void GUIRTVisualisation::setSize(int w, int h)
{
	//store width and height
	_width = w;
	_height = h;

	//if a buffer exist already, delete it
	if(_buffer != 0)
		delete[] _buffer;

	//calculate the new buffer size
	_bufferSize = w * h * 3;

	//creat the new buffer
	_buffer = new char[_bufferSize];
	memset(_buffer, 255, sizeof(char) * _bufferSize);

	//if the frame doesn't exist, create a new frame
	if (_frame == 0)
		_frame = new frmRenderedPicture;

	//give the buffer to the frame
	_frame->setBuffer(_buffer, w, h);
}

//display the frame
void GUIRTVisualisation::display()
{
	_frame->show();
}

//return the picture buffer
char* GUIRTVisualisation::getBuffer()
{
	return _buffer;
}

//get a pointer to the render picture widget
frmRenderedPicture* GUIRTVisualisation::getRenderFrame()
{
	return _frame;

}
